/* Fix CSS
 * http://code.google.com/p/html5andcss3/
 * Author: kael http://weibo.com/loskael
 */
package los.kael.file;

import java.util.Iterator;
import java.util.List;

/**
 * 文件合并
 * 
 * @author kael
 * 
 */
public class FileManage {
	private String root = "";
	private String outputFileName = "";
	private String filetype = "";
	private String include = "";
	private String charset = "UTF-8";
	private List<String> list = null;
	private StringBuffer sb = new StringBuffer();

	/**
	 * 文件合并 不指定编码和类型 只指定文件目录和输出文件名
	 * 
	 * @param root
	 * @param outputFilename
	 * @throws Exception
	 */
	public FileManage(String root, String outputFilename) throws Exception {
		this.root = root;
		this.outputFileName = outputFilename;
		list = new IteaFileToList(include, filetype).GetAllFilesByStringToList(root);
	}

	/**
	 * 文件合并 指定 目录 输出文件 文件类型
	 * 
	 * @param root
	 * @param outputFilename
	 * @param filetype
	 * @throws Exception
	 */
	public FileManage(String root, String outputFilename, String filetype) throws Exception {
		this.root = root;
		this.outputFileName = outputFilename;
		this.filetype = filetype;
		list = new IteaFileToList(include, filetype).GetAllFilesByStringToList(root);
	}

	/**
	 * 文件合并 指定 目录 输出文件 文件类型和包含关键字
	 * 
	 * @param root
	 * @param outputFilename
	 * @param filetype
	 * @param include
	 * @throws Exception
	 */
	public FileManage(String root, String outputFilename, String filetype, String include) throws Exception {
		this.root = root;
		this.outputFileName = outputFilename;
		this.filetype = filetype;
		this.include = include;
		list = new IteaFileToList(include, filetype).GetAllFilesByStringToList(root);
	}

	/**
	 * 指定 目录 输出文件 文件类型 包含关键字和编码
	 * 
	 * @param root
	 * @param outputFilename
	 * @param filetype
	 * @param include
	 * @param charset
	 * @throws Exception
	 */
	public FileManage(String root, String outputFilename, String filetype, String include, String charset)
			throws Exception {
		this.root = root;
		this.outputFileName = outputFilename;
		this.filetype = filetype;
		this.include = include;
		this.charset = charset;
		list = new IteaFileToList(include, filetype).GetAllFilesByStringToList(root);
	}

	/**
	 * 合并输出
	 * 
	 * @throws Exception
	 */
	public FileManage Merge() throws Exception {
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			sb.append(new FileReaderToStringBuffer(it.next().toString()).getSb());
		}
		new FileWriterFromStringBuffer(sb, outputFileName, charset);
		return this;
	}

	/**
	 * 合并输出
	 * 
	 * @throws Exception
	 */
	public FileManage Merge(String outputFileName) throws Exception {
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			sb.append(new FileReaderToStringBuffer(it.next().toString()).getSb());
		}
		new FileWriterFromStringBuffer(sb, outputFileName, charset);
		return this;
	}

	/**
	 * 逐个输出
	 * 
	 * @throws Exception
	 */
	public FileManage OneByOne() throws Exception {
		return OneByOne(null);
	}

	/**
	 * 逐个输出
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public FileManage OneByOne(String name) throws Exception {
		Iterator<String> it = list.iterator();
		String filename;
		while (it.hasNext()) {
			String temp = it.next().toString();
			sb.append(new FileReaderToStringBuffer(temp).getSb());
			if (name == null) {
				new FileWriterFromStringBuffer(sb, temp, charset);
			} else {
				int index = temp.indexOf('.');
				if (index != -1) {
					filename = temp.substring(0, index) + "." + name + "." + temp.substring(index + 1);
				} else {
					filename = temp + "." + name;
				}
				new FileWriterFromStringBuffer(sb, filename, charset);
			}
		}
		return this;
	}

}
